<?php

/**
 * 前台 新闻 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: show.class.php 48 2011-07-18 09:57:45Z zwt007 $
 * http://www.t-mac.org；
 */
class show extends Model
{

    private $article_table;
    private $category_table;
    private $addonarticle_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->article_table = '#@__article';
        $this->category_table = '#@__category';
        $this->addonarticle_table = '#@__addonarticle';
    }

    /**
     * 增加一次click
     * @param <type> $id
     * @return <type> 
     */
    public function saveArticleClick($id)
    {
        $sql = "UPDATE $this->article_table SET click_count = click_count+1 WHERE article_id = '$id'";
        $rs = $this->db->execute($sql);
        return $rs;
    }

    /**
     * 取栏目信息
     * @param <type> $nicename
     * @return <type>
     */
    public function getCategoryInfo($nicename)
    {
        $sql = "SELECT * FROM $this->category_table WHERE category_nicename = '$nicename' ORDER BY cat_order DESC, cat_id DESC LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        return $rs;
    }

    /**
     * 取栏目信息
     * @param <type> $nicename
     * @return <type>
     */
    public function getCategoryInfoById($cat_id)
    {
        $sql = "SELECT * FROM $this->category_table WHERE cat_id = '$cat_id' ORDER BY cat_order DESC, cat_id DESC LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        return $rs;
    }

    /**
     * 取出资讯无分页
     * @param <type> $cat_id
     * @return <type>
     */
    public function getArticleListNoPage($cat_id)
    {
        $sql = "SELECT a.article_id , a.title, a.time, a.attachments, b.content FROM $this->article_table a LEFT JOIN $this->addonarticle_table b ON a.article_id = b.article_id WHERE a.type='post' AND a.cat_id = '$cat_id' ORDER BY orderby DESC, article_id DESC";
        $rs = $this->db->getAll($sql);
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['time'] = date('Y-m-d', $v['time']);
            }
        }

        $ErrorMsg = '';
        $count = count($rs);
        if ($count == 0) {
            $ErrorMsg = "暂无资讯! ";
        }
        
        $this->assign('rs', $rs);
        return $rs;
    }

}